<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="名字" prop="name">
        <el-input
          style="width: 200px"
          v-model="queryParams.name"
          placeholder="请输入名字"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系方式" prop="phone">
        <el-input
          style="width: 200px"
          v-model="queryParams.phone"
          placeholder="请输入联系方式"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否展示" prop="isShow">
        <el-select
          v-model="queryParams.isShow"
          placeholder="请选择是否展示"
          style="width: 200px"
          clearable
        >
          <el-option
            :label="item.dictLabel"
            :value="item.dictValue"
            v-for="item in sysYesNo"
            :key="item.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否常见" prop="isCommon">
        <el-select
          v-model="queryParams.isCommon"
          placeholder="请选择是否常见"
          style="width: 200px"
          clearable
        >
          <el-option
            :label="item.dictLabel"
            :value="item.dictValue"
            v-for="item in sysYesNo"
            :key="item.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['index:message:add']"
          >新增</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['index:message:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['index:message:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="messageList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="名字" align="center" prop="name" />
      <el-table-column label="留言内容" align="center" prop="question" />
      <el-table-column label="联系方式" align="center" prop="phone" />
      <el-table-column label="是否展示">
        <template #default="scope">
          <el-switch
            v-model="scope.row.isShow"
            active-value="Y"
            inactive-value="N"
            active-text="是"
            inactive-text="否"
            style="
              --el-switch-on-color: #13ce66;
              --el-switch-off-color: #ff4949;
            "
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="是否常见">
        <template #default="scope">
          <el-switch
            v-model="scope.row.isCommon"
            active-value="Y"
            inactive-value="N"
            active-text="是"
            inactive-text="否"
            style="
              --el-switch-on-color: #13ce66;
              --el-switch-off-color: #ff4949;
            "
            @change="handleStatusChanges(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="留言回复" align="center" prop="reply" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['index:message:edit']"
            >回复</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['index:message:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改留言咨询对话框 -->
    <el-drawer :title="title" v-model="open" size="40%" append-to-body>
      <el-form ref="messageRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="留言内容" prop="question" class="formtextarea">
          <el-input
            class="inputs"
            v-model="form.question"
            type="textarea"
            placeholder="请输入内容"
            disabled
          />
        </el-form-item>
        <el-form-item label="名字" prop="name">
          <el-input disabled v-model="form.name" placeholder="请输入名字" />
        </el-form-item>
        <el-form-item label="联系方式" prop="phone">
          <el-input
            disabled
            v-model="form.phone"
            placeholder="请输入联系方式"
          />
        </el-form-item>
        <el-form-item label="留言回复" prop="reply">
          <el-input
            v-model="form.reply"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-drawer>
  </div>
</template>

<script setup name="Message">
import {
  listMessage,
  getMessage,
  delMessage,
  addMessage,
  updateMessage,
} from "@/api/index/message";
import { getDicts } from "@/api/system/dict/data";
const { proxy } = getCurrentInstance();

const messageList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    question: null,
    name: null,
    phone: null,
    reply: null,
    isShow: null,
    isCommon: null,
    valiFlag: null,
  },
  rules: {
    question: [
      { required: true, message: "留言内容不能为空", trigger: "blur" },
    ],
    phone: [{ required: true, message: "联系方式不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询留言咨询列表 */
function getList() {
  loading.value = true;
  listMessage(queryParams.value).then((response) => {
    messageList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    question: null,
    name: null,
    phone: null,
    reply: null,
    isShow: null,
    isCommon: null,
    createTime: null,
    createBy: null,
    updateTime: null,
    updateBy: null,
    valiFlag: null,
  };
  proxy.resetForm("messageRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加留言咨询";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getMessage(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改留言咨询";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["messageRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateMessage(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addMessage(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
// 表格开关状态（是否展示）
function handleStatusChange(row) {
  if (row.reply == null) {
    let text = row.isShow === "Y" ? "启用" : "停用";
    proxy.$modal
      .confirm('确认要"' + text + '""' + row.id + '"吗?')
      .then(function () {
        updateMessage({
          id: row.id,
        });
      })
      .then(() => {
        proxy.$modal.msgError(`请先回复留言内容`);
      })
      .catch(function () {
        row.isShow = row.isShow === "Y" ? "N" : "Y";
      });
    getList();
  } else {
    let text = row.isShow === "Y" ? "启用" : "停用";
    proxy.$modal
      .confirm('确认要"' + text + '""' + row.id + '"吗?')
      .then(function () {
        updateMessage({
          id: row.id,
          isShow: row.isShow,
        });
      })
      .then(() => {
        proxy.$modal.msgSuccess(text + "成功");
      })
      .catch(function () {
        row.isShow = row.isShow === "Y" ? "N" : "Y";
      });
  }
}
// 表格开关状态(是否常见)
function handleStatusChanges(row) {
  if (row.reply == null) {
    let text = row.isCommon === "Y" ? "启用" : "停用";
    proxy.$modal
      .confirm('确认要"' + text + '""' + row.id + '"吗?')
      .then(function () {
        updateMessage({ id: row.id });
      })
      .then(() => {
        proxy.$modal.msgError(`请先回复留言内容`);
      })
      .catch(function () {
        row.isCommon = row.isCommon === "N" ? "N" : "N";
      });
    getList();
  } else {
    let text = row.isCommon === "Y" ? "启用" : "停用";
    proxy.$modal
      .confirm('确认要"' + text + '""' + row.id + '"吗?')
      .then(function () {
        updateMessage({ id: row.id, isCommon: row.isCommon });
      })
      .then(() => {
        proxy.$modal.msgSuccess(text + "成功");
      })
      .catch(function () {
        row.isCommon = row.isCommon === "Y" ? "N" : "Y";
      });
  }
}
/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除留言咨询编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delMessage(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "index/message/export",
    {
      ...queryParams.value,
    },
    `message_${new Date().getTime()}.xlsx`
  );
}
const sysYesNo = ref();
function getsysYesNoList() {
  getDicts("sys_yes_no").then((res) => {
    if (res.code == 200) {
      sysYesNo.value = res.data;
    }
  });
}
getsysYesNoList();
getList();
</script>
<style lang="scss" scoped>
.formtextarea {
  ::v-deep .el-textarea__inner {
    -webkit-appearance: none;
    background-color: var(--el-input-bg-color, var(--el-fill-color-blank));
    background-image: none;
    border: none;
    border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
    box-shadow: 0 0 0 1px var(--el-input-border-color, var(--el-border-color))
      inset;
    box-sizing: border-box;
    color: var(--el-input-text-color, var(--el-text-color-regular));
    display: block;
    font-family: inherit;
    font-size: inherit;
    line-height: 1.5;
    padding: 5px 11px;
    position: relative;
    resize: vertical;
    transition: var(--el-transition-box-shadow);
    width: 100%;
    height: 200px;
  }
}
</style>
